package com.cloud.rest;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: PageInfoRes
 * @Description: 格式化的分页数据类
 * @Version: v1.0.0
 * @Author: yangyuwei
 * @Date: 2020/1/15 9:15
 * Modification History:
 * Date             Author      Version     Description
 * -------------------------------------------------------------
 * 2020/1/15      yangyuwei      v1.0.0      创建
 */
@Data
@Accessors(chain = true)
@ApiModel(value = "分页数据响应格式类")
public class PageInfoRes<T> {

    @ApiModelProperty(value = "响应数据列表")
    private List<T> content;
    @ApiModelProperty(value = "响应数据大小")
    private Long totalElements;
    @ApiModelProperty(value = "响应数据实际页码")
    private Integer page;

    public static<T> PageInfoRes build(PageInfo<T> pageInfo){
        PageInfoRes<T> pageInfoRes = new PageInfoRes<T>();
        pageInfoRes.setContent(pageInfo.getList()).setTotalElements(pageInfo.getTotal()).setPage(pageInfo.getPageNum());
        return pageInfoRes;
    }

    public static<T> PageInfoRes build(List<T> list, Integer page){
        PageInfoRes<T> pageInfoRes = new PageInfoRes<T>();
        pageInfoRes.setContent(list).setPage(page);
        return pageInfoRes;
    }

    public static<T> PageInfoRes empty(){
        PageInfoRes<T> pageInfoRes = new PageInfoRes<T>();
        pageInfoRes.setContent(new ArrayList<>()).setTotalElements(0L);
        return pageInfoRes;
    }
}
